package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.domain.ShoppingTrolley;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.SignQueryObject;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.UserOrder;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ISignService;
import cn.wolfcode.trip.base.service.IShoppingTrolleyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserIntegralService;
import cn.wolfcode.trip.base.service.IUserOrderService;
import cn.wolfcode.trip.base.query.*;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.service.ICollectService;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ICollectService collectService;
    @Autowired
    private IPrivateMessageService privateMessageService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IPraiseService praiseService;
    @Autowired
    private IUserOrderService userOrderService;
    @Autowired
    private IShoppingTrolleyService shoppingTrolleyService;
    @Autowired
    private ISignService signService;
    @Autowired
    private IUserIntegralService userIntegralService;


    /*
     *获取某个用户的所有购物车内容
     * 资源:/users/{id}/shoppings
     * 动作:GET
     * 参数:currentPage=x
     **/
    @GetMapping("/{id}/shoppings")
    public Object getShopping(@PathVariable Long id,QueryObject qo){
        PageInfo<ShoppingTrolley> info = shoppingTrolleyService.selectShoppingsById(id,qo);
        return info;
    }

    @GetMapping("/{userId}/signs")
    public Object query(SignQueryObject qo) {
        Long userId = qo.getUserId();
        qo.setPageSize(0);
        PageInfo<Sign> info = signService.addquery(qo,userId);
        return info;
    }

    @PostMapping("/{userId}/signs")
    public Object saveSigns(@PathVariable Long userId) {
        signService.save(userId);
        return new JSONResult();
    }

    @PostMapping("/{userId}/userIntegrals")
    public Object updateUserIntegrals(@PathVariable Long userId,Integer integral) {
        userIntegralService.update(userId,integral);
        return new JSONResult();
    }


    /*
    * 更新某个用户的游记
    * 资源:/{authorId}/travels
    * 动作:PUT
    * 参数:(travels对象)
    * */
    @PutMapping("/{authorId}/travels")
    public Object updateTravels(Travel entity){
        entity.setAuthor(UserContext.getCurrentUser());
        travelService.saveOrUpdate(entity);
        return new JSONResult();
    }

    /*
     *获取某个用户的所有游记(带有分页功能)
     * 资源:/users/{authorId}/travels
     * 动作:GET
     * 参数:currentPage=x
     **/
    @GetMapping("/{authorId}/travels")
    public Object queryInfo(TravelQueryObject qo){
        qo.setOrderBy("t.lastUpdateTime desc");
        PageInfo<Travel> info = travelService.query(qo);
        return info;
    }

    /*
    * 某个用户游记保存
    * 资源:/users/{authorId}/travels
    * 动作:POST
    * 参数:(travels的需要编辑的一系列参数)
    * */
    @PostMapping("/{authorId}/travels")
    public Object saveTravels(Travel entity){
        entity.setAuthor(UserContext.getCurrentUser());
        travelService.saveOrUpdate(entity);
        return new JSONResult();
    }

    //注册
    /*
      注册账号
    * 资源:/users
    * 动作:POST
    * 参数:email:xx
    *     password:xx
    *     nickName:xx
    * */
    @PostMapping
    public Object regist(User u){
        JSONResult result = new JSONResult();
        try{
            userService.regist(u);
            //设置回调函数的参数
            result.setData(u);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /*
    *更新用户信息
    * 资源:/users/{id}
    * 动作:PUT
    * 参数:nickName=xx&gender=xx&place=xx&sign=xx&headImgUrl=xx&coverImgUrl=xx
    **/
    @PutMapping("/{id}")
    public Object updateUser(User u){
        JSONResult result = new JSONResult();
        try{
            userService.saveOrUpdate(u);
            //设置回调函数的参数
            result.setData(u);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /*
     * 根据id获取某个用户的所有信息
     * 资源:/users/{id}
     * 动作:GET
     * 参数:currentPage=x
     **/
    @GetMapping("/{id}")
    public Object get(@PathVariable Long id){
        return userService.get(id);
    }

    /*
     *获取某个用户的已经收藏的游记(带有分页功能)
     * 资源:/users/{id}/collect/travels
     * 动作:GET
     * 参数:userId=x & type=3
     **/
    @GetMapping("/{id}/collect/travels")
    public Object queryCollectTravels(CollectQueryObject qo){
        qo.setOrderBy("collectTime desc");
        PageInfo<Collect> info = collectService.query(qo);
        return info;
    }

    /*
     *获取某个用户的已经收藏的攻略(带有分页功能)
     * 资源:/users/{id}/collect/strategies
     * 动作:GET
     * 参数:userId=x & collect=1 & currentPage=x
     **/
    @GetMapping("/{id}/collect/strategies")
    public Object queryCollectStrategies(CollectQueryObject qo){
        qo.setOrderBy("collectTime desc");
        PageInfo<Collect> info = collectService.query(qo);
        return info;
    }

    /*
     *获取某个用户的已经收藏的日报(带有分页功能)
     * 资源:/users/{id}/collect/news
     * 动作:GET
     * 参数:userId=x & state=1 & currentPage=x
     **/
    @GetMapping("/{id}/collect/news")
    public Object queryCollectNews(CollectQueryObject qo){
        qo.setOrderBy("collectTime desc");
        PageInfo<Collect> info = collectService.query(qo);
        return info;
    }

    /*
     *获取某个用户的所有订单(带有分页功能)
     * 资源:/users/{id}/orders
     * 动作:GET
     * 参数:currentPage=x
     **/
    @GetMapping("/{id}/orders")
    public Object getOrder(QueryObject qo){
        PageInfo<UserOrder> info = userOrderService.query(qo);
        return info;
    }

    /*
     *保存订单信息
     * 资源:/users/orders
     * 动作:POST
     * 参数:UserOrder
     **/
    @PostMapping("/orders")
    public Object saveOrder(UserOrder userOrder){
        userOrderService.saveOrUpdate(userOrder);
        return new JSONResult();
    }

    /*
     *保存购物车信息
     * 资源:/users/{id}/shoppings
     * 动作:POST
     * 参数:UserOrder
     **/
    @PostMapping("/shoppings")
    public Object saveShopping(ShoppingTrolley empty){
        shoppingTrolleyService.saveOrUpdate(empty);
        return new JSONResult();
    }


    /**
     * 获取某个用户信息
     * 参数:用户id
     */
    @GetMapping()
    public Object getUser(Long id) {
        User user = userService.get(id);
        return user;
    }

    /**
     * 获取某用户的所有粉丝
     * 参数:用户id
     * 返回值:user列表
     */
    @GetMapping("/{userId}/fans")
    public Object queryFans(UserQueryObject qo) {
        qo.setPageSize(10);
        qo.setType("fans");
        return userService.queryFollowAndFan(qo);
    }

    /**
     * 获取某用户的所有关注的人
     * 参数:用户id
     * 返回值:user列表
     */
    @GetMapping("/{userId}/follows")
    public Object queryFollows(UserQueryObject qo) {
        qo.setPageSize(10);
        return userService.queryFollowAndFan(qo);
    }

    /**
     * 获取某个用户的收藏(日报,攻略,游记,不包括点评)
     */
    @GetMapping("/{userId}/collects")
    public Object queryCollects(CollectQueryObject qo) {
        qo.setOrderBy("collectTime desc");
        qo.setPageSize(5);
        return collectService.query(qo);
    }

    /*查询该用户所有消息.
     包括已读,未读,时间倒序,如果未读,有个标记
      * */
    @GetMapping("/{userId}/privateMessages")
    public Object queryPrivateMessages(PrivateMessageQueryObject qo) {
        qo.setOrderBy("sendTime desc");
        qo.setPageSize(8);
        PageInfo pageInfo = privateMessageService.queryPrivateMessage(qo);
        return pageInfo;
    }

    /*查询某用户所有的评论(攻略的)*/
    @GetMapping("/{userId}/comments")
    public Object queryComments(StrategyCommentQueryObject qo) {
        qo.setOrderBy("sc.releaseTime desc");
        qo.setPageSize(5);
        return strategyCommentService.selectForUser(qo);
    }


    /**
     * 查询某用户收到的点赞(游记 或 评论)
     */
    @GetMapping("/{userId}/praises")
    public Object queryPraise(PraiseQueryObject qo) {
        qo.setPageSize(8);
        PageInfo pageInfo = praiseService.queryPraiseByReceiverId(qo);
        return pageInfo;
    }

    /*查询某用户收到的评论(游记或者攻略评论的)*/

    @GetMapping("/{userId}/receiveComments")
    public Object queryReceiveComments(StrategyReplyQueryObject qo) {
        qo.setPageSize(8);
        PageInfo pageInfo = userService.queryReceiveComments(qo);
        return pageInfo;
    }

    /*查询某用户所有的未读消息总数
    * */
    @GetMapping("/{userId}/counts")
    public Object queryForNoReadCount(@PathVariable Long userId){
        return userService.countForAllNoRead(userId);
    }
}